<?php namespace App\Admin\Auth;

use Auth, Input;
use App\Core\Repository\Criterias\FixedCriteria;
use App\Admin\Access\User\Repository as UserRepository;

trait UserCenterService {
    
    //基础服务
    use \App\Core\Service\Service;
    use \App\Core\Service\Services\Indexer;
    
    protected function bootService(){
        $this->bootBaseService();
        $this->setRepository(new UserRepository);
        $this->repository = $this->getRepository();
    }
    
    //获取用户信息
    public function user(){
        $user = Auth::user();
        if(!$user){
            return view('admin.login');
        }
        $user->load('role');
        return $this->success($user);
    }
    
    //获取用户权限
    public function permission(){
        $user = Auth::user();
        $operations = [];
        $groups = [];
        foreach($user->getOperations() as $val){
            $arr = explode('@', $val['code']);
            $carr = explode('\\', $arr[0]);
            foreach($carr as $key => $val){
                $carr[$key] = lcfirst($val);
            }
            $operations[] = str_replace('Controller', '', implode('.', $carr).'.'.$arr[1]);
        }
        foreach($user->getOperationGroups() as $val){
            $groups[] = $val['code'];
        }
        return $this->success([
            'operations' => $operations,
            'groups' => $groups
        ]);
    }
    
    //修改资料
    public function editProfile(){
        $user = Auth::user();
        $nickname = Input::get('nickname', '');   
		if(strlen($nickname) < 1){
			return $this->inputError('昵称不能为空');
		}
        try {
            $this->repository->resetCriteria()->addCriteria(new FixedCriteria('user_id', $user->user_id))->update([
                'nickname' => $nickname,
                'icon' => Input::get('icon', ''),
                'name' => Input::get('name', ''),
                'phone' => Input::get('phone', ''),
                'email' => Input::get('email', ''),
                'introduction' => Input::get('introduction', '')
            ]);
            return $this->success();
		} catch (\Exception $ex) {
            return $this->sysError($ex);
		}
    }
    
    //重置密码
    public function resetPassword(){
        $user = Auth::user();
        $passwordOld = Input::get('password_old', '');
        $passwordNew = Input::get('password_new', '');
		if(!$this->validatePassword($passwordOld)){
			return $this->inputError('原密码错误');
		}
        $saltcode = $saltcode = rand(10000, 99999);
        $password = md5($passwordNew.$saltcode);
        try {
            $this->repository->resetCriteria()->addCriteria(new FixedCriteria('user_id', $user->user_id))->update([
                'saltcode' => $saltcode,
                'password' => $password
            ]);
            return $this->success();
		} catch (\Exception $ex) {
            return $this->sysError($ex);
		}
    }
	
	//注销
	public function logout(){
		Auth::logout();
        return $this->success('登出成功');
	}
    
    private function validatePassword($password){
        $user = Auth::user();
		if($user->islocked == '1'){
			return false;
		}
		if(md5($password.$user->saltcode) == $user->password){//验证密码成功
			return true;
		}
        return false;
    }
}